﻿using System;
using System.Linq;
using Invoicing.Model;
using Invoicing.Model.Orders;
using Invoicing.Model.Members;
using Invoicing.Model.Products;
using Utils;

namespace Invoicing.Command.Orders
{
    public class BookOrderCommand
    {
        public static BookOrderModel NewOrder(BookOrderModel order)
        {
            if (order != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var orderMethod = (OrderMethod)Enum.Parse(typeof(OrderMethod), order.Method.ToString());
                    order.OrderNo = Comm.Orders.CreateOrderNo(db, orderMethod);
                    var dOrder = Convertor.ConvertFrom<BookOrder, BookOrderModel>(order);
                    var dItems = Convertor.ConvertFromCollection<BookOrderDetail, BookOrderDetailModel>(order.Items);
                    foreach (var it in dItems)
                        dOrder.BookOrderDetails.Add(it);
                    db.DataContext.BookOrders.Add(dOrder);

                    db.DataContext.SaveChanges();
                    order.ID = dOrder.ID;
                    return order;
                }
            }
            return null;
        }

        public static void UpdateOrder(BookOrderModel order)
        {
            if (order != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var existOrder = db.DataContext.BookOrders.FirstOrDefault(p => p.OrderNo == order.OrderNo);
                    if (existOrder != null)
                    {
                        //existOrder.BookOrderDetails.Clear();
                        db.Delete<BookOrderDetail>(p => p.OID == existOrder.ID);
                        Convertor.UpdateValue(order, existOrder);
                        var dItems = Convertor.ConvertFromCollection<BookOrderDetail, BookOrderDetailModel>(order.Items);
                        foreach (var it in dItems)
                            existOrder.BookOrderDetails.Add(it);
                        db.DataContext.SaveChanges();
                    }
                }
            }
        }
        /// <summary>
        /// 查询单据列表
        /// </summary>
        /// <param name="sea"></param>
        /// <returns></returns>
        public static BookOrderCollection GetOrders(BookOrderQuery sea)
        {
            var result = new BookOrderCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<BookOrder> q = db.DataContext.BookOrders;
                if (sea != null)
                {
                    if (!string.IsNullOrEmpty(sea.OrderNo))
                        q = q.Where(p => p.OrderNo == sea.OrderNo);
                    if (sea.CustomerID > 0)
                        q = q.Where(p => p.CustomerID == sea.CustomerID);
                    if (sea.OperatorUserID > 0)
                        q = q.Where(p => p.OperatorUserID == sea.OperatorUserID);
                    if (sea.OrderDateBefore != null)
                        q = q.Where(p => p.NoteDate.CompareTo(sea.OrderDateBefore.Value) >= 0);
                    if (sea.OrderDateAfter != null)
                        q = q.Where(p => p.NoteDate.CompareTo(sea.OrderDateAfter.Value) <= 0);
                    if (sea.Method != null && sea.Method.Length > 0)
                        q = q.Where(p => sea.Method.Contains(p.Method));
                    if (sea.OrderStatus != null && sea.OrderStatus.Length > 0)
                        q = q.Where(p => sea.OrderStatus.Contains(p.OrderStatus));
                }
                var n = q.Count();
                if (n > 0)
                {
                    q = q.OrderByDescending(p => p.ID);
                    if (sea != null && sea.PageSize > 0)
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    var lst = q.ToList();
                    result = Convertor.ConvertCollection<BookOrderModel, BookOrder, BookOrderCollection>(lst);
                    result.TotalRecordsCount = n;
                    foreach (var m in result)
                    {
                        var d = lst.Find(p => p.OrderNo == m.OrderNo);
                        if (sea.GetCustomer)
                            m.Customer = Convertor.ConvertTo<CustomerModel, Customer>(d.Customer);
                        if (sea.GetOperator)
                            m.Operator = d.User.RealName;
                        if (sea.GetItems)
                        {
                            m.Items = Convertor.ConvertCollection<BookOrderDetailModel, BookOrderDetail, BookOrderDetailCollection>(d.BookOrderDetails);
                            if (sea.GetProduct)
                            {
                                foreach (var it in m.Items)
                                {
                                    var dit = d.BookOrderDetails.First(p => p.ID == it.ID);
                                    it.Product = Convertor.ConvertTo<ProductModel, Product>(dit.Product);
                                }
                            }
                        }
                    }
                }
            }
            return result;
        }
        /// <summary>
        /// 查询单张单据
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        public static BookOrderModel GetOrder(string orderNo)
        {
            if (!string.IsNullOrEmpty(orderNo))
            {
                using (DBCentre db = new DBCentre())
                {
                    var d = db.DataContext.BookOrders.FirstOrDefault(p => p.OrderNo == orderNo);
                    if (d != null)
                    {
                        var m = Convertor.ConvertTo<BookOrderModel, BookOrder>(d);
                        m.Customer = Convertor.ConvertTo<CustomerModel, Customer>(d.Customer);
                        m.Items = Convertor.ConvertCollection<BookOrderDetailModel, BookOrderDetail, BookOrderDetailCollection>(d.BookOrderDetails);
                        foreach (var o in m.Items)
                        {
                            var di = d.BookOrderDetails.FirstOrDefault(p => p.ID == o.ID);
                            o.Product = Convertor.ConvertTo<ProductModel, Product>(di.Product);
                            o.Material = Convertor.ConvertTo<ProductMaterialModel, ProductMaterial>(di.ProductMaterial);
                            o.Process = Convertor.ConvertTo<ProductProcessModel, ProductProcess>(di.ProductProcess);
                        }
                        return m;
                    }
                }
            }
            return null;
        }
        /// <summary>
        /// 撤消交易单据
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        public static void DeleteOrder(string orderNo)
        {
            if (!string.IsNullOrEmpty(orderNo))
            {
                using (DBCentre db = new DBCentre())
                {
                    db.Delete<BookOrder>(p => p.OrderNo == orderNo);
                    db.DataContext.SaveChanges();
                }
            }
        }
    }
}
